package edu.gatech.oad.mule.view;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;

import edu.gatech.oad.mule.view.interfaces.RenderablePlayer;
import edu.gatech.oad.mule.view.interfaces.RenderablePlayer.PlayerRace;

/**
 * Contains a set of all image paths used in the game, so that image paths can
 * be changed in one place should an image be renamed. Also contains methods for
 * constructing images from game data.
 * 
 * @author Dale Avery
 * 
 */
final class GraphicsLib {
	
	// Class instance for Singleton design pattern
	private static GraphicsLib instance;
	
	// Screens
	public static final String SCREEN_INTRO = "edu/gatech/oad/mule/graphics/screens/SCREEN_INTRO.png";

	// Player Images
	public static final String PLAYER_BONZOID_STATIONARY = "edu/gatech/oad/mule/graphics/screens/PLAYER_BONZ_STATIONARY.png";
	public static final String PLAYER_BUZZ_STATIONARY = "edu/gatech/oad/mule/graphics/screens/PLAYER_BUZZ_STATIONARY.png";
	public static final String PLAYER_FLAPPER_STATIONARY = "edu/gatech/oad/mule/graphics/screens/PLAYER_FLAP_STATIONARY.png";
	public static final String PLAYER_HUMAN_STATIONARY = "edu/gatech/oad/mule/graphics/screens/PLAYER_HUMA_STATIONARY.png";
	public static final String PLAYER_UGAITE_STATIONARY = "edu/gatech/oad/mule/graphics/screens/PLAYER_UGAI_STATIONARY.png";
	
	// Robot Images
	public static final String ROBOT_MULE = "edu/gatech/oad/mule/graphics/screens/ROBOT_MULE.png";
	
	/**
	 * Builds a new instance of GraphicsLib by loading in all Mule graphics.
	 */
	private GraphicsLib() {
		
	}
	
	/**
	 * @return A singleton instance of GraphicsLib.
	 */
	public GraphicsLib getInstance() {
		if (instance == null)
			instance = new GraphicsLib();
		return instance;
	}

	/**
	 * Builds a custom image for a given player.
	 * 
	 * @param player
	 *            The player for which an image will be built.
	 * @return A custom image built based on the player's data
	 */
	public static Image buildPlayerImage(RenderablePlayer player) {
		Image sprite;
		if (player != null && player.getRace() != PlayerRace.ERROR) {
			ClassLoader classLoader = Thread.currentThread()
					.getContextClassLoader();
			InputStream input;

			// Load the player's image based on their race.
			try {
				if (player.getRace() == PlayerRace.BONZOID) {
					input = classLoader
							.getResourceAsStream(GraphicsLib.PLAYER_BONZOID_STATIONARY);
				} else if (player.getRace() == PlayerRace.BUZZITE) {
					input = classLoader
							.getResourceAsStream(GraphicsLib.PLAYER_BUZZ_STATIONARY);
				} else if (player.getRace() == PlayerRace.FLAPPER) {
					input = classLoader
							.getResourceAsStream(GraphicsLib.PLAYER_FLAPPER_STATIONARY);
				} else if (player.getRace() == PlayerRace.HUMAN) {
					input = classLoader
							.getResourceAsStream(GraphicsLib.PLAYER_HUMAN_STATIONARY);
				} else if (player.getRace() == PlayerRace.UGAITE) {
					input = classLoader
							.getResourceAsStream(GraphicsLib.PLAYER_UGAITE_STATIONARY);
				} else {
					throw new Exception("Unknown player race ("
							+ player.getRace()
							+ ") entered. Could not load player image.");
				}
				sprite = ImageIO.read(input);
				return sprite;
			} catch (IOException e) {
				System.out.println("Failed to read in image for player "
						+ player.getName() + ".");
				e.printStackTrace();
			} catch (Exception e) {
				System.out.println(e.getMessage());
				e.printStackTrace();
			}
		}
		return null;
	}

}
